package com.aabte.spring.validator.customize;

import java.util.regex.Pattern;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/4/25
 */
public class IdentityCardNumberValidator implements
    ConstraintValidator<IdentityCardNumber, Object> {

  private static final Pattern PATTERN =
      Pattern.compile(
          "^[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$");

  @Override
  public void initialize(IdentityCardNumber constraintAnnotation) {

  }

  @Override
  public boolean isValid(Object value, ConstraintValidatorContext context) {
    return isValidate18IdCard(value.toString());
  }

  private boolean isValidate18IdCard(String idCard) {
    return PATTERN.matcher(idCard).matches();
  }
}
